//
//  Digital.swift
//  xzseller
//
//  Created by zhuxietong on 2021/2/6.
//

import Foundation
import UIKit
extension Float{
    public var validDigital:String{
        get{let formatter = NumberFormatter()
            formatter.groupingSeparator = ""
            formatter.decimalSeparator = "."
            formatter.numberStyle = .decimal
            formatter.formatterBehavior = .default
            formatter.roundingMode = .down
            formatter.maximumFractionDigits = 2
            formatter.minimumFractionDigits = 0
            let num = NSNumber(floatLiteral: Double("\(self)") ?? 0)
            if let n = formatter.string(from: num) {
                return n
                
            }
           return "0"
        }
    }
}
extension CGFloat{
    public var validDigital:String{
        get{
            let formatter = NumberFormatter()
            formatter.groupingSeparator = ""
            formatter.decimalSeparator = "."
            formatter.numberStyle = .decimal
            formatter.formatterBehavior = .default
            formatter.roundingMode = .down
            formatter.maximumFractionDigits = 2
            formatter.minimumFractionDigits = 0
            let num = NSNumber(floatLiteral: Double("\(self)") ?? 0)
            if let n = formatter.string(from: num) {
                return n
                
            }
           return "0"
        }
    }
}

extension String{
    public var validDigital:String{
        get{
            let formatter = NumberFormatter()
            formatter.groupingSeparator = ""
            formatter.decimalSeparator = "."
            formatter.numberStyle = .decimal
            formatter.formatterBehavior = .default
            formatter.roundingMode = .down
            formatter.maximumFractionDigits = 2
            formatter.minimumFractionDigits = 0

            let num = NSNumber(floatLiteral: Double("\(self)") ?? 0)
            if let n = formatter.string(from: num) {
                return n
            }
           return "0"
        }
    }
    
    public var pretty:String{
        get{
            let formatter = NumberFormatter()
            formatter.groupingSeparator = ""
            formatter.decimalSeparator = "."
            formatter.numberStyle = .decimal
            formatter.formatterBehavior = .default
            formatter.roundingMode = .down
            formatter.maximumFractionDigits = 2
            formatter.minimumFractionDigits = 0

            var uni = ""
            var price = Double("\(self)") ?? 0
            if price > 1000{
                price = price / 1000
                formatter.maximumFractionDigits = 0
                uni = "k"
            }
            let num = NSNumber(floatLiteral: price)
            
            if let n = formatter.string(from: num) {
                return n + uni
            }
           return "0"
        }
    }
    public var shotPretty:String{
        get{
            let formatter = NumberFormatter()
            formatter.groupingSeparator = ""
            formatter.decimalSeparator = "."
            formatter.numberStyle = .decimal
            formatter.formatterBehavior = .default
            formatter.roundingMode = .down
            formatter.maximumFractionDigits = 2
            formatter.minimumFractionDigits = 0

            let uni = ""
            let price = Double("\(self)") ?? 0
//            if price > 1000{
//                price = price / 1000
//                formatter.maximumFractionDigits = 0
//                uni = "k"
//            }
            let num = NSNumber(floatLiteral: price)
            
            if let n = formatter.string(from: num) {
                return n + uni
            }
           return "0"
        }
    }
}
